var satchmo_tuning = satchmo_tuning || {};

satchmo_tuning.qty_changed = function(evt) {
	data = {}
	data[this.name] = this.value;
	$.post("/shop/quickorder/ajax/", data, satchmo_tuning.quick_order_success, "json");
}

satchmo_tuning.update_product_list = function(items_in_cart) {
	$("[@name^=qty__]").val('');
	$("[@class*=in_cart]").removeClass('in_cart');
	for (var slug in items_in_cart) {
		$("[@name=qty__" + slug + "]").val(items_in_cart[slug]);
		$("#prod__" + slug).addClass('in_cart');
	}
}

satchmo_tuning.quick_order_success = function(responseText, statusText) {
	if (responseText.cart_total) {
		$("#cart_totals_count").html(responseText.cart_count);
		$("#cart_totals_total").html(" - " + responseText.cart_total);
		$("#cart_totals").show();
	} else {
		$("#cart_totals").hide();
	}
	satchmo_tuning.update_product_list(responseText.items_in_cart);
	$("#cart_total").html(responseText.cart_total);
}

satchmo_tuning.setup_add_to_cart_form = function() {
	var options = {
		url: "/shop/quickorder/ajax/",
		dataType: "json",
		success: function(responseText, statusText) {
			satchmo_tuning.quick_order_success(responseText, statusText);
			if ($("#cart_totals").is(':hidden')) {
				alert('Корзина очищена!');
			} else {
				alert('Корзина обновлена!');
			}
		}
	};
	$("#add_to_cart_form").ajaxForm(options);
	$("[@name^=qty__]").change(satchmo_tuning.qty_changed);
}
